package com.sdp.irrigation.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.db.irrigation_eq.service.IIrrigationEqService;
import com.sdp.irrigation.db.irrigation_eq_repair.service.IIrrigationEqRepairService;
import com.sdp.irrigation.db.irrigation_eq_repair.service.impl.IrrigationEqRepairServiceImpl;
import com.sdp.irrigation.db.irrigation_pump_well.service.IIrrigationPumpWellService;
import com.sdp.irrigation.entity.IrrigationEqRepair;
import com.sdp.irrigation.utils.CurrentLoginEmp;
import com.sdp.irrigation.utils.IrrigationConstants;
import com.sdp.irrigation.view.IrrigationEqRepairView;

/**
 * <p>
 * 设备报修信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-02-02
 */
@RestController
@RequestMapping("/IrrigationEqRepairController")
public class IrrigationEqRepairController {
	
	@Autowired
	private IIrrigationEqRepairService iIrrigationEqRepairService;
	
	@Autowired
	private IIrrigationEqService iIrrigationEqService;
	
	@Autowired
	private IIrrigationPumpWellService iIrrigationPumpWellService;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@Autowired
	private IrrigationEqRepairServiceImpl irrigationEqRepairServiceImpl;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iIrrigationEqRepairService, IrrigationEqRepair.class, dataCenter));
	}
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<IrrigationEqRepairView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(irrigationEqRepairServiceImpl, IrrigationEqRepairView.class, dataCenter,queryWrapper));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody IrrigationEqRepair repair) {
		if(repair==null) {
			return Result.error("报修信息为空");
		}
		if(StringUtils.isEmpty(repair.getKeyId())) {
			repair.setCreateTime(LocalDateTime.now());
			repair.setUpdateTime(LocalDateTime.now());
			JSONObject emp = currentLoginEmp.getCurrentLoginInfo();
			repair.setOperatorName(emp.getString("empName"));
			repair.setOperatorNo(emp.getString("empNo"));
			repair.setIsRepair(IrrigationConstants.否.getCode());
			iIrrigationEqRepairService.save(repair);
			iIrrigationEqService.updateEqRepairStatusByEqNo(repair.getEqNo(), IrrigationConstants.设备报修状态_已报修.getCode());
			iIrrigationPumpWellService.updateEqRepairStatusByEqNo(repair.getEqNo(), IrrigationConstants.设备报修状态_已报修.getCode());
		}else {
			repair.setUpdateTime(LocalDateTime.now());
			iIrrigationEqRepairService.updateById(repair);
		}
		return Result.ok(repair);
	}

	/**
	 * 设备维修
	 * @param repair
	 * @return
	 */
	@PostMapping(value = "/repair")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> repair(@RequestBody IrrigationEqRepair repair) {
		if(repair==null) {
			return Result.error("维修信息为空");
		}
		IrrigationEqRepair dbRepair = iIrrigationEqRepairService.getById(repair.getKeyId());
		if(StringUtils.isEmpty(dbRepair)) {
			return Result.error("此报修信息已不存在");
		}
		if(IrrigationConstants.是.getCode().equals(dbRepair.getIsRepair())) {
			return Result.error("此报修信息已维修");
		}
		repair.setUpdateTime(LocalDateTime.now());
		repair.setRepairFinishTime(LocalDateTime.now());
		repair.setIsRepair(IrrigationConstants.是.getCode());
		iIrrigationEqRepairService.updateById(repair);
		
		iIrrigationEqService.updateEqRepairStatusByEqNo(repair.getEqNo(), IrrigationConstants.设备报修状态_未报修.getCode());
		iIrrigationPumpWellService.updateEqRepairStatusByEqNo(repair.getEqNo(), IrrigationConstants.设备报修状态_未报修.getCode());
		return Result.ok(repair);
	}
}

